package com.yungu.swift.api.route.v2.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.gateway.CurrentUser;
import com.yungu.swift.api.config.NacosListener;
import com.yungu.swift.api.utils.CommonUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiParam;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.order.model.param.ApiOrderPayParam;
import com.yungu.swift.order.model.vo.PayVo;
import com.yungu.swift.order.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;
import static com.yungu.swift.constants.CommonConstant.*;
import static com.yungu.swift.constants.OrderConstant.*;

/**
 * @author : cuixiuyin
 * @date : 2019/11/21
 */
@RestController
@RequestMapping("/api/v2/passenger/token/pay")
@Api(value = "【乘客】【支付】乘客支付接口V2", tags = "V2-乘客支付接口")
public class PasPayApi {

    @Reference
    private PayService payService;

    @PostMapping(value = {"/wallet"})
    @ApiOperation(value = "乘客余额支付", notes = "返回成功或失败")
    public ResponseData<Map<String, String>> walletPay(@RequestBody ApiOrderPayParam pasPayParam) {
        if (StringUtils.isEmpty(pasPayParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return payService.trade(new PayVo(PAY_PURPOSE_CHARGE_FARE, PASSENGER, PAY_TYPE_BALANCE, pasPayParam.getUserUuid(),
                pasPayParam.getOrderUuid(), pasPayParam.getCouponUuid(), null, pasPayParam.getAppid(),
                null, null));
    }


    @PostMapping(value = {"/alipay/wallet"})
    @ApiOperation(value = "乘客支付宝充值")
    public ResponseData<String> walletAliPay(@RequestBody Map<String, String> paramMap,
                                             @CurrentUser ApiParam apiParam, HttpServletRequest request) {
        // 充值面额Id
        String rechargeId = paramMap.get("rechargeId");
        if (StringUtils.isEmpty(rechargeId)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        ResponseData<Map<String, String>> trade = payService.trade(new PayVo(PAY_PURPOSE_RECHARGE_ACCOUNT, PASSENGER,
                PAY_TYPE_ALIPAY, apiParam.getUserUuid(), null, null, rechargeId,
                apiParam.getAppid(), CommonUtil.getUserIP(request), null));
        if (trade.isSuccess() && trade.getData() != null) {
            return ResponseData.buildSuccessResponse(trade.getData().get("url"));
        }
        return ResponseData.buildErrorResponse(trade.getErrCode(), trade.getMsg());
    }

    @PostMapping(value = "/alipay/tradeUrl")
    @ApiOperation(value = "乘客支付宝支付【APP】", notes = "乘客支付宝APP支付接口")
    public ResponseData<String> aliTradeUrl(@RequestBody ApiOrderPayParam pasPayParam, HttpServletRequest request) {
        if (StringUtils.isEmpty(pasPayParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        ResponseData<Map<String, String>> trade = payService.trade(new PayVo(PAY_PURPOSE_CHARGE_FARE, PASSENGER,
                PAY_TYPE_ALIPAY, pasPayParam.getUserUuid(), pasPayParam.getOrderUuid(), pasPayParam.getCouponUuid(),
                null, pasPayParam.getAppid(), CommonUtil.getUserIP(request), null));
        if (trade.isSuccess() && trade.getData() != null) {
            return ResponseData.buildSuccessResponse(trade.getData().get("url"));
        }
        return ResponseData.buildErrorResponse(trade.getErrCode(), trade.getMsg());
    }


    @PostMapping(value = {"/wx/wallet"})
    @ApiOperation(value = "乘客微信充值")
    public ResponseData<Map<String, String>> walletWxPay(@RequestBody Map<String, String> paramMap,
                                                         @CurrentUser ApiParam apiParam, HttpServletRequest request) {
        // 充值面额Id
        String rechargeId = paramMap.get("rechargeId");
        if (StringUtils.isEmpty(rechargeId)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return payService.trade(new PayVo(OrderConstant.PAY_PURPOSE_RECHARGE_ACCOUNT, PASSENGER, PAY_TYPE_TENPAY,
                apiParam.getUserUuid(), null, null, rechargeId, apiParam.getAppid(),
                CommonUtil.getUserIP(request), null));
    }

    @PostMapping(value = "/wx/tradeUrl")
    @ApiOperation(value = "乘客微信支付【APP】", notes = "乘客微信APP支付接口")
    public ResponseData<Map<String, String>> wxTradeUrl(@RequestBody ApiOrderPayParam pasPayParam, HttpServletRequest request) {
        if (StringUtils.isEmpty(pasPayParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return payService.trade(new PayVo(PAY_PURPOSE_CHARGE_FARE, PASSENGER, PAY_TYPE_TENPAY, pasPayParam.getUserUuid(),
                pasPayParam.getOrderUuid(), pasPayParam.getCouponUuid(), null, pasPayParam.getAppid(),
                CommonUtil.getUserIP(request), null));
    }


    @PostMapping(value = "/wx/applet/tradeUrl")
    @ApiOperation(value = "乘客微信支付【小程序】", notes = "乘客微信小程序支付接口")
    public ResponseData<Map<String, String>> wxAppletTradeUrl(@RequestBody ApiOrderPayParam pasPayParam, HttpServletRequest request) {
        if (StringUtils.isEmpty(pasPayParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        String appletWxAppid = NacosListener.WXPAY_CONFIG.getPassengerWxAppid();
        return payService.trade(new PayVo(PAY_PURPOSE_CHARGE_FARE, PASSENGER_APPLET, PAY_TYPE_TENPAY,
                pasPayParam.getUserUuid(), pasPayParam.getOrderUuid(), pasPayParam.getCouponUuid(), null,
                appletWxAppid, CommonUtil.getUserIP(request), pasPayParam.getOpenid()));
    }

    @PostMapping(value = "/wx/public/tradeUrl")
    @ApiOperation(value = "乘客微信支付【公众号】", notes = "乘客微信公众号支付接口")
    public ResponseData<Map<String, String>> wxPublicTradeUrl(@RequestBody ApiOrderPayParam pasPayParam, HttpServletRequest request) {
        if (StringUtils.isEmpty(pasPayParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        String publicWxAppid = NacosListener.WXPAY_CONFIG.getPassengerPublicWxAppid();
        return payService.trade(new PayVo(PAY_PURPOSE_CHARGE_FARE, PASSENGER_PULBIC, PAY_TYPE_TENPAY,
                StringUtils.trimToEmpty(pasPayParam.getUserUuid()), pasPayParam.getOrderUuid(), pasPayParam.getCouponUuid(),
                null, publicWxAppid, CommonUtil.getUserIP(request), pasPayParam.getOpenid()));
    }

    @PostMapping(value = "/wx/h5/tradeUrl")
    @ApiOperation(value = "乘客微信支付【H5】", notes = "乘客H5微信支付接口")
    public ResponseData<Map<String, String>> wxH5TradeUrl(@RequestBody ApiOrderPayParam pasPayParam, HttpServletRequest request) {
        String h5WxAppid = NacosListener.WXPAY_CONFIG.getPassengerH5WxAppid();
        return payService.trade(new PayVo(PAY_PURPOSE_CHARGE_FARE, PASSENGER_H5, PAY_TYPE_TENPAY, pasPayParam.getUserUuid(),
                pasPayParam.getOrderUuid(), pasPayParam.getCouponUuid(), null, h5WxAppid,
                CommonUtil.getUserIP(request), null));
    }
}
